package com.yvan.serverless.jdbc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;

public class TransactionService {
    //依赖spring-boot-starter-jdbc 注入DataSourceTransactionManager
    @Autowired
    private PlatformTransactionManager transactionManager;

    public TransactionStatus begin() {
        //默认的传播级别
        TransactionStatus transaction = transactionManager.getTransaction(new DefaultTransactionAttribute());
        return transaction;
    }

    public void commit(TransactionStatus transaction) {
        transactionManager.commit(transaction);
    }

    public void rollback(TransactionStatus transaction) {
        transactionManager.rollback(transaction);
    }

}
